//
//  ZWPropertyDetailController.m
//  NewProject
//
//  Created by tongtong on 2018/5/18.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZWPropertyDetailController.h"
#import "ZWProDetailHeadView.h"
#import "ZWProDetailCell.h"
#import "ZWPropertyReceiveController.h"
#import "ZWPropertyPayController.h"
#import "ZWalletInfo.h"
@interface ZWPropertyDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) ZWProDetailHeadView *headView;
@property (nonatomic,strong) UIButton *receiveBtn;//收款
@property (nonatomic,strong) UIButton *payBtn;//付款
@property(nonatomic,assign)BOOL  reload;
@property(nonatomic,strong) ZWalletInfoList*item;
@end

@implementation ZWPropertyDetailController
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    
//    [self requestCoinNumberData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _headView = [[ZWProDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, Scare_Width(192))];
    _headView.backgroundColor = [UIColor colorWithHexString:@"#fafafa"];
    self.tableView.tableHeaderView = _headView;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoback)];
    self.view.backgroundColor = COLOR_WHITE;
    [self.view addSubview:self.tableView];
    
    UIButton *btn = [[UIButton alloc]init];
    btn.frame = CGRectMake(Scare_Width(10), Scare_Width(25), 44, 44);
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(gotoback) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [self.payBtn setTitle:Z_Location(@"send") forState:UIControlStateNormal];
    [self.receiveBtn setTitle:Z_Location(@"receive") forState:UIControlStateNormal];

    [self.view addSubview:self.receiveBtn];
    [self.view addSubview:self.payBtn];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestCoinNumberData)];
    [self.tableView.mj_header beginRefreshing];
//    [self requestCoinNumberData];
}

-(void)requestCoinNumberData{
    if(self.reload){
        return;
    }
//    ZWalletAccount *wallet = [ZWalletAccount wallet];
    NSString *url = [NSString stringWithFormat:@"%@wallet/queryBalance",PortServer];
    NSDictionary *param  = @{@"address":self.walletAccount.walletAdress,
                             @"currency":self.currency
                             };
    ZWLog(@"url = %@\n param = %@",url,param);
    ZWWeakSelf;
    self.reload = YES;
    [ZNetManager GET:url parameters:param success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
        [weakSelf.tableView.mj_header endRefreshing];
        ZWLog(@"responseObject = %@",responseObject);
        self.reload = NO;
        NSInteger status = [responseObject[@"status"] integerValue];
        if(status == 1){
            weakSelf.item = [ZWalletInfoList mj_objectWithKeyValues:responseObject[@"data"]];
            [weakSelf.headView configWithName:weakSelf.currency currencyUnit:[ZWalletAccount wallet].currentCurrrencyUnit money:weakSelf.item.balance cny:weakSelf.item.cny.stringValue];
            [weakSelf.tableView reloadData];
        }else
        {
            [ZProgressManager showError:responseObject[@"message"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        self.reload = NO;
        [ZProgressManager showError:@"连接失败"];
        ZWLog(@"error = %@",error.userInfo);
    }];
}
-(void)gotoback{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark --收钱--
-(void)receiveMoneyMethod{
    ZWPropertyReceiveController *receive = [[ZWPropertyReceiveController alloc]init];
    receive.walletInfo = self.walletAccount;
    [self presentViewController:receive animated:YES completion:nil];
}
#pragma mark --付款--
-(void)payMoneyMethod{
    ZWPropertyPayController *payControl = [[ZWPropertyPayController alloc]init];
    payControl.curreny = self.currency;
    payControl.walletInfo = self.walletAccount;
    [self presentViewController:payControl animated:YES completion:nil];

}
#pragma mark - tableView datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Scare_Width(82);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc]init];
    backView.backgroundColor = COLOR_WHITE;
    backView.frame = CGRectMake(0, 0, SCREEN_W, Scare_Width(50));
    
    UILabel *label = [[UILabel alloc]init];
//    label.text = nil;
    label.frame = CGRectMake(Scare_Width(15), Scare_Width(15), SCREEN_W - Scare_Width(30), Scare_Width(20));
//    label.text = Z_Location(@"transactionLog");
    label.font = H_Medium3;
    label.textColor = [UIColor colorWithHexString:@"#cccccc"];
    [backView addSubview:label];
    
    return backView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        cell.textLabel.text = @"暂无记录～";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor colorWithHexString:@"cccccc"];
        return cell;
    }
  
    ZWProDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZWProDetailCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = COLOR_WHITE;
    [cell setDate:@"12-12" address:@"dfkdkfkfdfjks" transactionNum:@(0.212)];
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return Scare_Width(50);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]init];
}

#pragma mark ---set----
- (UITableView *)tableView {
    if (!_tableView) {
        
        CGFloat nav = iPhoneX ? 44 : 20;
        CGFloat bottom = iPhoneX ? 34 : 0;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -nav, SCREEN_W, SCREEN_H -bottom -Scare_Width(50)) style:0];

       
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_WHITE;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZWProDetailCell class] forCellReuseIdentifier:@"ZWProDetailCell"];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

    }
    return _tableView;
}
-(UIButton *)receiveBtn{
    if (!_receiveBtn) {
        CGFloat bottom = iPhoneX ? 34 : 0;
        _receiveBtn = [[UIButton alloc]init];
        _receiveBtn.frame = CGRectMake(0, SCREEN_H - Scare_Width(50) - bottom, SCREEN_W/2, Scare_Width(50));
        [_receiveBtn setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
        [_receiveBtn addTarget:self action:@selector(receiveMoneyMethod) forControlEvents:UIControlEventTouchUpInside];
        _receiveBtn.backgroundColor = [UIColor colorWithHexString:@"38b7cc"];
        [_receiveBtn setImage:[UIImage imageNamed:@"receive"] forState:UIControlStateNormal];
    }
    return _receiveBtn;
}
-(UIButton *)payBtn{
    if (!_payBtn) {
        _payBtn = [[UIButton alloc]init];
        CGFloat bottom = iPhoneX ? 34 : 0;
        _payBtn.frame = CGRectMake(SCREEN_W/2, SCREEN_H - Scare_Width(50) - bottom, SCREEN_W/2, Scare_Width(50));
        [_payBtn setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
        _payBtn.backgroundColor = [UIColor colorWithHexString:@"046cba"];
        [_payBtn addTarget:self action:@selector(payMoneyMethod) forControlEvents:UIControlEventTouchUpInside];
        [_payBtn setImage:[UIImage imageNamed:@"send"] forState:UIControlStateNormal];
    }
    return _payBtn;
}
@end
